package site.assad.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import site.assad.domain.User;
import site.assad.service.UserService;

import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.util.List;


/**
 * Author: Al-assad 余林颖
 * E-mail: yulinying_1994@outlook.com
 * Date: 2017/12/19 18:37
 * Description:
 */

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    //转发到 JSP 视图

    @RequestMapping("/register")
    public String register(){
        return "/user/jsp/register";
    }

    @RequestMapping("/showUserList")
    public ModelAndView showUserList(){
        List<User> userList = userService.getAllUser();
        return new ModelAndView("/user/jsp/userList","userList",userList);
    }

    @RequestMapping(value="/handleRegister",method=RequestMethod.POST)
    public String register1(@Valid @ModelAttribute("user") User user, BindingResult bindingResult,Model model) {
        if(bindingResult.hasErrors()){
            return "/user/jsp/register";
        }
        boolean flag = userService.registerUser(user);
        if(flag){
            user = userService.getUserByName(user.getUserName());
            model.addAttribute("user",user);
            return "/user/jsp/userDetail";
        }else{
            bindingResult.rejectValue("userName","repeated");
            return "/user/jsp/register";
        }
    }

    //转发到 FreeMarker 模板视图
    @RequestMapping("/showUserListByFtl")
    public ModelAndView showUserListByFtl(){
        List<User> userList = userService.getAllUser();
        return new ModelAndView("/user/ftl/userListFtl","userList",userList);
    }

    //转发到 Excel 视图
    @RequestMapping("/showUserListByExcel")
    public ModelAndView showUserListByExcel(){
        List<User> userList = userService.getAllUser();
        return new ModelAndView("userListExcel");
    }

    //转发到 PDF 视图
    @RequestMapping("/showUserListByPDF")
    public ModelAndView showUserListByPDF(){
        List<User> userList = userService.getAllUser();
        return new ModelAndView("userListPDF");
    }

    //转发到 XML 视图
    @RequestMapping("/showUserListByXML")
    public ModelAndView showUserListByXML(){
        List<User> userList = userService.getAllUser();
        return new ModelAndView("userListXML");
    }

    //转发到 JSON 视图
    @RequestMapping("/showUserListByJSON")
    public ModelAndView showUserListByJSON(){
        List<User> userList = userService.getAllUser();
        return new ModelAndView("userListJSON");
    }

    //使用同一URL获取不同格式的返回内容
    @RequestMapping("/showUserListMix")
    public ModelAndView showUserListMix(){
        List<User> userList = userService.getAllUser();
        return new ModelAndView("userListMix");
    }





}
